﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03工厂方法设计模式
{
 
    public interface ICalFactory
    {
        ICalculator GetCalculator();
    }
    [OperFactory("*")]
    public class MulFactory : ICalFactory
    {
        public ICalculator GetCalculator()
        {
            return new Mul();
        }
    }
    [OperFactory("+")]
    public class AddFactory : ICalFactory
    {
        public ICalculator GetCalculator()
        {
            return new Add();
        }
    }
    [OperFactory("-")]
    public class SubFactory : ICalFactory
    {
        public ICalculator GetCalculator()
        {
            return new Sub();
        }
    }
    [OperFactory("/")]
    public class DivFactory : ICalFactory
    {
        public ICalculator GetCalculator()
        {
            return new Div();
        }
    }

/*-----------------------------------------------------------------------------------------*/
    /// <summary>
    ///  静态工作方法 实际上就是把创建对象的过程放到静态方法里面
    /// </summary>
    public class CalFactory
    {
        public static ICalculator GetCalculator(string oper)
        {
            ICalculator? calculator = null;
            switch (oper)
            {
                case "*":
                    calculator = new Mul();
                    break;
                case "+":
                    calculator = new Add();
                    break;
                case "-":
                    calculator = new Sub();
                    break;
                case "/":
                    calculator = new Div();
                    break;
            }
            return calculator;
        }
    }
}
